package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableRowSorter;

import com.neu.dao.impl.ProblemDao;
import com.neu.pojo.Model;
import com.neu.pojo.Problem;
import com.neu.pojo.Worker;
import com.neu.service.impl.ModelService;
import com.neu.service.impl.ProblemService;
import com.neu.table.ProblemTable;

import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.JButton;
import javax.swing.JComboBox;

public class AddProblemUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private ProblemTable problemTable;
	private ProblemService problemService = ProblemService.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddProblemUI frame = new AddProblemUI(new Worker(),new Model());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddProblemUI(Worker w,Model m) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 764, 533);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		this.problemTable = new ProblemTable();
		table = new JTable(this.problemTable);
		table.setBounds(25, 137, 696, 281);
		contentPane.add(table);
		
		JButton btnNewButton = new JButton("删除");
		btnNewButton.setBounds(43, 104, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				problemTable.delete();//删除选中行的对象
				
				problemTable.fireTableDataChanged();
				JOptionPane.showMessageDialog(null, "删除成功");//提示删除成功
			}
		});
		
		JButton btnNewButton_1 = new JButton("新增");
		btnNewButton_1.setBounds(602, 104, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			//新增问题
			CreateProblemUI createProblemUI = new CreateProblemUI(w, m);//跳转到新窗口
			createProblemUI.setVisible(true);
			setVisible(false);
			
		});
		
		JButton btnNewButton_2 = new JButton("详情");
		btnNewButton_2.setBounds(487, 104, 97, 23);
		contentPane.add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//查看问题详情
				int sum = 0,tar=0;
				for(int i=0;i<problemTable.getRowCount();i++) {//统计勾选的行数
					if((boolean)problemTable.getValueAt(i, 0)) {
						tar = (int) problemTable.getValueAt(i, 1);//得到id
						sum++;
					}
				}
				if(sum!=1) {
					JOptionPane.showMessageDialog(null, "请单选");//提示用户只能单选
				}else {
					Problem p = problemService.search(new Problem(tar));//用id搜索，获取问题信息
					JOptionPane.showMessageDialog(null,"问题ID:"+p.getId()+" 题目:"+p.getProblem()+" 类型:"+p.getType()+" 选项:"+p.getOptionA()+" "+p.getOptionB()+" "+p.getOptionC() );
				}
				
			}
		});
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(43, 45, 97, 32);
		contentPane.add(comboBox);
		comboBox.addItem("所有");
		comboBox.addItem("A");
		comboBox.addItem("B");
		comboBox.addItem("C");
		comboBox.addActionListener(new ActionListener() {//由下拉列表展示不同类型对象
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableRowSorter sorter = new TableRowSorter(problemTable);
				table.setRowSorter(sorter);
				String s=(String) comboBox.getSelectedItem();
				
				switch (s) {
			    	case "所有":
			    		break;
			    	case "A":
			    		List<RowFilter<Object, Object>> filter=new ArrayList<>(2);
				    	filter.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter));
				    	break;
			    	case "B":
				    	List<RowFilter<Object, Object>> filter1=new ArrayList<>(2);
				    	filter1.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter1));
				    	break;
				    case "C":
				    	List<RowFilter<Object, Object>> filter2=new ArrayList<>(2);
				    	filter2.add(RowFilter.regexFilter(s));
				    	sorter.setRowFilter(RowFilter.andFilter(filter2));
				    	break;
				    default:
				    	break;
				}
			}
		});
		
		JButton btnNewButton_3 = new JButton("添加");
		btnNewButton_3.setBounds(170, 440, 97, 23);
		contentPane.add(btnNewButton_3);
		btnNewButton_3.addActionListener((e)->{
			
			ArrayList<Problem> problems = new ArrayList<>();
			
			//选中的对象加到模板中 调用service模板传进去
			for(int i=0;i<this.problemTable.getRowCount();i++) {
				if((boolean)this.problemTable.getValueAt(i, 0)) {
					problems.add(new Problem((int)this.problemTable.getValueAt(i, 1)));
				}
			}
			if(ModelService.getInstance().getLink(m, problems)) {
				JOptionPane.showMessageDialog(null, "添加成功");
			}else {
				JOptionPane.showMessageDialog(null, "添加失败");
			}
			
		});
		
		JButton btnNewButton_3_1 = new JButton("返回");
		btnNewButton_3_1.setBounds(451, 440, 97, 23);
		contentPane.add(btnNewButton_3_1);
		btnNewButton_3_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				CheckModel checkModel = new CheckModel(w,m);
				checkModel.setVisible(true);
				setVisible(false);
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table);
		jp.setBounds(25, 137, 696, 281);
		contentPane.add(jp);
	}
}
